<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				relateCacheKey: "bro-yq-tsampleBorrowDelay-relate-cache"
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods: {
			formSubmit(){
				let _that = this
				_that.om.formSave("tsampleBorrowDelay/addTsampleBorrowDelay",_that.formdata,_that)
			},
			getCodeInfo() {
				let _that = this
				_that.om.request(
					"tsampleBorrowDelay/getCodeInfo", {
						code: _that.$refs.autoform.formdata.relateCode
					}
				).then(res => {
					_that.subEditorObj["productInfo"].workList = res.value
				})
			},
			initLoad(options) {
				let _that = this
				let ajaxArr = [
					urlUtil.zposUrl(options.code)
				]
				let fileId = options.fileId
				if(fileId){
					ajaxArr.push({
						url:"tsampleBorrowDelay/getInfo",
						param:{
							id:fileId
						},
						method:"post"
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("topinfo", "zposId", "data", configSelectData(res[0], "id", "posname"), mainData)
					changeEditor("title1", "relateCode", {
						link: "../../public/pageSearch/pageSearch",
						"linkParam": {
							other: JSON.stringify({}),
							url: "tsampleBorrowDelay/getCodeList",
							namefield: "code",
							cachename: _that.relateCacheKey,
							initload: true,
							extrafield: "customerUserName",
							notefield: "zuserName",
							placeholder: "请输入编码"
						}
					}, null, mainData)
					if (_that.subEditorObj["productInfo"]) {
						let att = {
							showAdd:false
						}
						let configData = _that.subConfigData["productInfo"]
						if (hasEditor("productInfo", "productCodes", configData)) {
							let title = getEditorTitle("productInfo", "productCodes", configData)
							att.titleTemplate =title+":<%= productCodes || '未填写' %>"
						}
						if(hasEditor("productInfo", "productName", configData)){
							let title = getEditorTitle("productInfo", "productName", configData)
							att.titleTemplate = att.titleTemplate + '|'+title+":<%= productName || '未填写' %>"
						}
						if(hasEditor("productInfo", "productModel", configData)){
							let title = getEditorTitle("productInfo", "productModel", configData)
							att.titleTemplate =att.titleTemplate + '|'+title+":<%= productModel || '未填写' %>"
						}
						if(hasEditor("productInfo", "productUnitName", configData)){
							let title = getEditorTitle("productInfo", "productUnitName", configData)
							att.subtitle1Template =title+":<%= productUnitName || '未填写' %>"
						}
						if(hasEditor("productInfo", "productNumber", configData)){
							let title = getEditorTitle("productInfo", "productNumber", configData)
							att.subtitle1Template =att.subtitle1Template + '|'+title+":<%= productNumber || '未填写' %>"
						}
						if(hasEditor("productInfo", "productMoneyOne", configData)){
							let title = getEditorTitle("productInfo", "productMoneyOne", configData)
							att.subtitle1Template =att.subtitle1Template + '|'+title+":<%= productMoneyOne || '未填写' %>"
						}
						changeEditor("productInfo", "objStr", att, mainData)
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[1].value.tsampleBorrowDelay){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value.tsampleBorrowDelay[index])
								_that.$set(_that.formdata,index,res[1].value.tsampleBorrowDelay[index])
							}
						}
						let dataList = _that._.cloneDeep(res[1].value.tsampleBorrowDelay_data)
						_that.subEditorObj["productInfo"].workList = dataList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"objStr",JSON.stringify(dataList))
						_that.$set(_that.formdata,"objStr",JSON.stringify(dataList))
						delete res[1].value.dataList
						_that.$nextTick(function(){
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
						})
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}else{
							_that.$refs.autoform.initSelectIndex("topifno","zposId")
						}
					})
				}, options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onUnload() {
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				for(let fieldId in _that.subEditorObj){
					let editor = _that.subEditorObj[fieldId]
					uni.getStorage({
						key:getWorkCacheKey(fieldId),
						success:(res)=>{
							if(res.data){
								editor.workList = res.data
							}
							uni.removeStorage({
								key:getWorkCacheKey(fieldId)
							})
						}
					})
				}
				uni.getStorage({
					key: _that.relateCacheKey,
					success: (res) => {
						if (res.data) {
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.relateCode = d.code
							formdata.productDelayNum = d.productDelayNum
							formdata.customerCodes = d.customerCodes
							formdata.customerName = d.customerName
							formdata.customerAddress = d.customerAddress
							formdata.customerUser = d.customerUser
							formdata.customerMobile = d.customerMobile
							formdata.customerMoney = d.customerMoney
							formdata.customerUserName = d.customerUserName
							_that.getCodeInfo()
							uni.removeStorage({
								key: _that.relateCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>

<style></style>
